<?php

spl_autoload_register(
    function ($class) {
        static $classes = null;
        $path = __DIR__ . DIRECTORY_SEPARATOR . 'aop';
        $classes = _create_libs($path);
        if (isset($classes[$class])) {
            require $path . $classes[$class];
        }
    }

);
function _create_libs($path)
{
    $libfile = dirname($path) . '/aoplibs.php';
    if (is_file($libfile)) {
        return current(include($libfile));
    }
    $files = array_merge(
        glob($path . '/*.php', GLOB_BRACE),
        glob($path . '/schema/*.php'),
        glob($path . '/request/*.php')
    );
    $libs = [];
    foreach ($files as $k => $v) {
        $_p = pathinfo($v);
        $libs[$_p['filename']] = str_replace($path, '', $_p['dirname'] . '/' . $_p['basename']);
    }
    file_put_contents($libfile, '<?php' . PHP_EOL . 'return array(' . var_export($libs, true) . ')' . PHP_EOL . '?>');
    return $libs;
}
